<?php
/**
 * Created by PhpStorm.
 * Script Name: MessageObj.php
 * Create: 2020/6/9 下午10:47
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\platform\handler\mp;


use plugin\wechat\app\model\MpMaterial;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\Music;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Video;
use EasyWeChat\Kernel\Messages\Voice;

class MessageObj
{
    /**
     * 图文
     * @param $media
     * @param null $mp_info
     * @return News
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fudaoji<fdj@kuryun.cn>
     * 18年10月12日起，被动回复消息与客服消息接口的图文消息类型中图文数目只能为一条 https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&announce_id=115383153198yAvN&version=&lang=zh_CN&token=
     */
    public static function news($media, $mp_info = null){
        is_null($mp_info) && $mp_info = request()->mpInfo;
        $items = [
            new NewsItem([
                'title'       => $media['title'],
                'description' => $media['digest'],
                'url'         => $media['source_url'],
                'image'       => $media['thumb_url'],
            ]),
        ];
        if($list = MpMaterial::where([
            ['company_id', '=', $mp_info['company_id']],
            ['mp_id', '=', $mp_info['id']],
            ['pid', '=', $media['id']]
        ])
            ->order('index',  'asc')
            ->field(['thumb_url', 'title'])
            ->select()) {
            foreach ($list as $vo) {
                array_push($items, new NewsItem([
                    'title'         => $vo['title'],
                    'description'   => $vo['digest'],
                    'url'           => $vo['source_url'],
                    'image'         => $vo['thumb_url'],
                ]));
            }
        }
        return new News($items);
    }

    /**
     * 音乐
     * @param $media
     * @return Music
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function music($media){
        return new Music([
            'title' => $media['title'],
            'description' => $media['digest'],
            'url' => $media['server_url'],
            'hq_url' => $media['source_url'],
            'thumb_media_id' => $media['thumb_media_id']
        ]);
    }

    /**
     * 视频
     * @param $media
     * @return Video
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function video($media){
        return new Video($media['media_id'], [
            'title' => $media['title'],
            'description' => $media['digest'],
        ]);
    }

    /**
     * 语音
     * @param $media
     * @return Voice
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function voice($media){
        return new Voice($media['media_id']);
    }

    /**
     * 文本
     * @param $media
     * @return Text
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function text($media){
        return new Text($media['content']);
    }

    /**
     * 图片
     * @param $media
     * @return Image
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function image($media){
        return new Image($media['media_id']);
    }
}